package com.fafian.gdc.app.android;

import android.app.Activity;
import android.app.Fragment;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

public class Configuration extends Activity {

	private static String server, apiKey;
	private static int port;
	private static TextView apiKeyTV, serverTV, portTV;
	private static Configuration instance;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_configuration);
		if (savedInstanceState == null) {
			getFragmentManager().beginTransaction()
					.add(R.id.container, new PlaceholderFragment()).commit();
		}
		SharedPreferences settings = getSharedPreferences(
				RemoteControl.SHARED_PREFERENCES_NAME, 0);
		server = settings.getString(GDCServerPreferences.SERVER_NAME, "");
		apiKey = settings.getString(GDCServerPreferences.APIKEY_NAME, "");
		port = settings.getInt(GDCServerPreferences.PORT_NUMBER_NAME, 0);
		instance = this;
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.configuration, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// Handle action bar item clicks here. The action bar will
		// automatically handle clicks on the Home/Up button, so long
		// as you specify a parent activity in AndroidManifest.xml.
		int id = item.getItemId();
		if (id == R.id.action_settings) {
			return true;
		}
		return super.onOptionsItemSelected(item);
	}
	
	private static void savePreferencesToStorage(){
		SharedPreferences settings = instance.getSharedPreferences(
				RemoteControl.SHARED_PREFERENCES_NAME, 0);
		Editor edit = settings.edit();
		edit.putString(GDCServerPreferences.APIKEY_NAME, 
				apiKeyTV.getText().toString());
		edit.putString(GDCServerPreferences.SERVER_NAME, 
				serverTV.getText().toString());
		edit.putInt(GDCServerPreferences.PORT_NUMBER_NAME, 
				Integer.parseInt(portTV.getText().toString()));
				
		edit.commit();
		
	}	


	/**
	 * A placeholder fragment containing a simple view.
	 */
	public static class PlaceholderFragment extends Fragment {

		public PlaceholderFragment() {
		}

		@Override
		public View onCreateView(LayoutInflater inflater, ViewGroup container,
				Bundle savedInstanceState) {
			View rootView = inflater.inflate(R.layout.fragment_configuration,
					container, false);
			apiKeyTV = (TextView) rootView.findViewById(R.id.api_key_edit);
			apiKeyTV.setText(apiKey);
			serverTV = (TextView) rootView.findViewById(R.id.server_edit);
			serverTV.setText(server);
			portTV = (TextView) rootView.findViewById(R.id.port_edit);
			portTV.setText(""+port);
			((Button) rootView.findViewById(R.id.send_button)).setOnClickListener(
					new View.OnClickListener() {
						
						@Override
						public void onClick(View v) {
							savePreferencesToStorage();
							instance.finish();
							
						}
					});
			return rootView;
		}
	}
}
